home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / TransferProvider / TransferProvider.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.4 KB  |  126 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        TransferProvider.c
  3.  
  4.     Contains:    Sample that shows how to use OTTransferProviderOwnership.
  5.  
  6.     Written by:     Quinn "The Eskimo!"    
  7.  
  8.     Copyright:    Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/23/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. ////////////////////////////////////////////////////////////
  25. // Pick up general Open Transport stuff.
  26.  
  27. #include <OpenTransport.h>
  28.  
  29. ////////////////////////////////////////////////////////////
  30. // Pick up OT debugging macros.
  31.  
  32. #include <OTDebug.h>
  33.  
  34. ////////////////////////////////////////////////////////////
  35. // Pick up standard C libraries.
  36.  
  37. #include <stdio.h>
  38.  
  39. ////////////////////////////////////////////////////////////
  40. // Get prototypes for the factory.
  41.  
  42. #include "ProviderFactory.h"
  43.  
  44. ////////////////////////////////////////////////////////////
  45. // OTDebugStr is not defined in any of the interfaces,
  46. // but it is in the libraries.
  47.  
  48. extern void OTDebugStr(char *str);
  49.  
  50. ////////////////////////////////////////////////////////////
  51.  
  52. static OSStatus GetProviderFromFactory(void)
  53.     // This routine calls FactoryCreateEndpoint to
  54.     // create an endpoint that is owned by the provider
  55.     // factory, and then uses OTTransferProviderOwnership
  56.     // to let Open Transport know that the newly created
  57.     // endpoint is owned by us.  It then does a simple
  58.     // operation an the endpoint, just to make sure
  59.     // everything works OK.
  60. {
  61.     OSStatus err;
  62.     EndpointRef originalEndpoint;
  63.     OTClient originalOwner;
  64.     EndpointRef newEndpoint;
  65.     TEndpointInfo newEndpointInfo;
  66.  
  67.     // Use the factory library to create an endpoint.
  68.     
  69.     err = FactoryCreateEndpoint(&originalEndpoint, &originalOwner);
  70.     
  71.     if (err == noErr) {
  72.     
  73.         // Transfer the ownership of that endpoint, so that OT knows
  74.         // we now own it.
  75.         
  76.         newEndpoint = OTTransferProviderOwnership(originalEndpoint, originalOwner, &err);
  77.  
  78.         if (err == noErr) {
  79.             
  80.             // We can now use newEndpoint as if we created it.  We call
  81.             // OTGetEndpointInfo as an example of an operation on the
  82.             // endpoint.
  83.  
  84.             err = OTGetEndpointInfo(newEndpoint, &newEndpointInfo);
  85.             
  86.             if (err == noErr) {
  87.                 printf("Maximum size of endpoint address = %ld.\n", newEndpointInfo.addr);
  88.             }
  89.             
  90.             OTCloseProvider(newEndpoint);
  91.         }
  92.     }
  93.     
  94.     return err;
  95. }
  96.  
  97. ////////////////////////////////////////////////////////////
  98.  
  99. void main(void)
  100.     // The main line.  This inits our connection to OpenTransport,
  101.     // and then inits the provider factory library.  It then
  102.     // calls GetProviderFromFactory to demo the use of
  103.     // OTTransferProviderOwnership.
  104. {
  105.     OSStatus err;
  106.     
  107.     err = InitOpenTransport();
  108.     if (err == noErr) {
  109.     
  110.         err = InitProviderFactory();
  111.         
  112.         if (err == noErr) {
  113.             
  114.             err = GetProviderFromFactory();
  115.  
  116.             CloseProviderFactory();
  117.         }
  118.     
  119.         CloseOpenTransport();
  120.     }
  121.     if (err == noErr) {
  122.         printf("Success!\n");
  123.     } else {
  124.         printf("Failed with error %ld.\n", err);
  125.     }
  126. }